package cn.hx.controller;

import cn.hx.entity.Dept;
import cn.hx.entity.DeptSchedule;
import cn.hx.service.DeptScheduleService;
import cn.hx.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DeptScheduleController {

    @Autowired
    DeptScheduleService deptScheduleService;
    @Autowired
    DeptService deptService;

    @GetMapping("searchDeptSchedule")
    public ModelAndView getSearchDept() {
        ModelAndView modelAndView = new ModelAndView("searchDeptSchedule");
        modelAndView.addObject("deptSchedules",deptScheduleService.findAll());
        return modelAndView;
    }
    @GetMapping("createDeptSchedule")
    public ModelAndView getCreateDeptSchedule(){
        ModelAndView modelAndView = new ModelAndView("createDeptSchedule");
        modelAndView.addObject("dept",deptService.findAll());
        return modelAndView;
    }

    //    修改回显
    @GetMapping("updateDeptSchedule/{id}")
    public String getUpdateDept(@PathVariable Integer id, Model model){
        model.addAttribute("deptSchedule",deptScheduleService.queryById(id));
        model.addAttribute("dept",deptService.findAll());
        return "updateDeptSchedule";
    }
    //    修改
    @PostMapping("editDeptSchedule/{id}")
    public String getEditDept(@PathVariable Integer id, DeptSchedule deptSchedule){
        deptSchedule.setId(id);
        deptScheduleService.update(deptSchedule);
        return "redirect:/searchDeptSchedule";
    }
    //    删
    @GetMapping("deleteDeptSchedule/{id}")
    public String getDeleteDept(@PathVariable Integer id){
        System.out.println(id);
        deptScheduleService.deleteById(id);
        return "redirect:/searchDeptSchedule";
    }

    //    增
    @PostMapping("saveDeptSchedule")
    public String getSaveDept(DeptSchedule deptSchedule){
        deptScheduleService.insert(deptSchedule);
        return "redirect:/searchDeptSchedule";
    }

}
